﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Domain.Interface;
using Domain.Model;
using Ds.DataBase;
using Ds.DataBase.Enum;
using Ds.DataBase.Model;
using Newtonsoft.Json;

namespace Domain.RealiZation
{
    public class Admin : IAdmin
    {
        public string ClickMyKaoHeBiaoOverflow()
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.MyKaoHeBiaoContext.Where(s => s.khbzt == 考核表状态.通过审核);
                var zw = db.ZhiWeiContext.ToList();
                List<JiaoZhiGongInfo> jzgs = new List<JiaoZhiGongInfo>();
                foreach (var item in data)
                {
                    var fzhz = 0m;
                    foreach (var item2 in item.MyKaoHeBiaoOnes)
                    {
                        if (item2.MyKaoHeBiaoTwos == null || item2.MyKaoHeBiaoTwos.Count == 0)
                        {
                            fzhz = fzhz + item2.fz;
                        }
                        else
                        {
                            foreach (var item3 in item2.MyKaoHeBiaoTwos)
                            {
                                fzhz = fzhz + item3.fz;
                            }
                        }

                    }

                    if (fzhz != 100)
                    {
                        JiaoZhiGongInfo model = new JiaoZhiGongInfo();
                        model = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == item.username);
                        jzgs.Add(model);
                    }

                }
                List<Domain_JiaoZhiGong> lst = new List<Domain_JiaoZhiGong>();
                foreach (var item in jzgs)
                {
                    Domain_JiaoZhiGong model = new Domain_JiaoZhiGong();
                    model.keshi = new Domain_KeShi();
                    model.keshi.ksid = item.keshi.ksid;
                    model.keshi.ksmc = item.keshi.ksmc;
                    model.ShouJiHao = item.ShouJiHao;
                    model.XingBie = item.XingBie;
                    model.XingMing = item.XingMing;
                    model.userinfoId = item.Users.userinfoId.ToString();
                    model.username = item.Users.username;
                    model.zgid = item.zgid;
                    model.zwid = item.kszw.zhiwu.zwid;
                    model.zwmc = item.kszw.zhiwu.zwmc;
                    lst.Add(model);
                }
                return JsonConvert.SerializeObject(lst);
            }
        }

        public void RejectGZJHByusername(string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.MyKaoHeBiaoContext.FirstOrDefault(s =>
                    s.username == username && s.khbzt == 考核表状态.通过审核);
                data.khbzt = 考核表状态.编辑中;
                db.SaveChanges();
            }
        }

        public void UpdateMyKaoHeBiaoWithGongZuoJiHua()
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.MyKaoHeBiaoContext;
                foreach (var item in data)
                {
                    var moban = db.GongZuoJiHuaContext.SingleOrDefault(s => s.mbid == item.mbid);
                    foreach (var one in moban.GongZuoJiHuaNeiRongs)
                    {
                        if (item.MyKaoHeBiaoOnes.All(s => s.mbnrid != one.ljid))
                        {
                            MyKaoHeBiao_one mykaohebiaoone = new MyKaoHeBiao_one();
                            mykaohebiaoone.mbnrid = one.ljid;
                            mykaohebiaoone.MyKaoHeBiao = item;
                            mykaohebiaoone.fz = one.gzjhfz;
                            mykaohebiaoone.htmlstr = one.gzjhhtml;
                            mykaohebiaoone.mbtype = 1;
                            mykaohebiaoone.mbnr = one.nr;
                            mykaohebiaoone.sx = one.gzjhsx;
                            mykaohebiaoone.MyKaoHeBiaoTwos = new List<MyKaoHeBiao_two>();
                            foreach (var two in one.GongZuoJiHuaSons)
                            {
                                MyKaoHeBiao_two myKaoHeBiaoTwo = new MyKaoHeBiao_two();
                                myKaoHeBiaoTwo.mbid = two.gzjhsid;
                                myKaoHeBiaoTwo.IsEdit = false;
                                myKaoHeBiaoTwo.MyKaoHeBiaoOne = mykaohebiaoone;
                                myKaoHeBiaoTwo.fz = two.gzjhsfz;
                                myKaoHeBiaoTwo.htmlstr = two.gzjhshtml;
                                myKaoHeBiaoTwo.mbnr = two.gzjhsnr;
                                myKaoHeBiaoTwo.sx = two.gzjhssx;
                                myKaoHeBiaoTwo.gzjhkssj = DateTime.Now;
                                myKaoHeBiaoTwo.gzjhwcsj = DateTime.Now;
                                myKaoHeBiaoTwo.bz = "admin";
                                mykaohebiaoone.MyKaoHeBiaoTwos.Add(myKaoHeBiaoTwo);
                            }
                            item.MyKaoHeBiaoOnes.Add(mykaohebiaoone);
                        }

                    }
                }

                db.SaveChanges();
            }
        }
    }
}
